﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using SQLite;
using System.Windows;
using System.Text.RegularExpressions;

namespace MOJsdk
{
    public class MojSystem
    {
        string _localFolder;
        private MojSystemVars _systemVars, _userVars;
        private MojDynamicVars _dynamicVars, _statusVars;
        private MojDrivePool _drivePool;
        private FileDataBaseInterop _filesDB;
        private MovieDataBaseInterop _moviesDB;
        private Languages_ISO639_1 _langPool;
        private MojPluginPool _pluginPool;
        private StaticFileCollection _tempFiles;

        public MojSystem(string ApplicationPath, IHost TheHost)
        {
            _localFolder = ApplicationPath;
            MojHost.GetHost = TheHost;
            _systemVars = new MojSystemVars(_localFolder, "SystemVars");

            INIFolders();
            INIData();
        }

        private void INIFolders()
        {
            CreateFolder(MojFoldersEnum.LanguagesFolder, Path.Combine(_localFolder, "Languages"));
            CreateFolder(MojFoldersEnum.PluginsFolder, Path.Combine(_localFolder, "Plugins"));
            CreateFolder(MojFoldersEnum.SkinsFolder, Path.Combine(_localFolder, "Skins"));
            CreateFolder(MojFoldersEnum.TemporaryFolder, Path.Combine(_localFolder, "Temp"));
            CreateFolder(MojFoldersEnum.DatabaseFolder, Path.Combine(_localFolder, "Databases"));
            CreateFolder(MojFoldersEnum.ImagesFolder, Path.Combine(_localFolder, "Images"));
            CreateFolder(MojFoldersEnum.LogsFolder, Path.Combine(_localFolder, "Logs"));
            CreateFolder(MojFoldersEnum.FiltersFolder, Path.Combine(_localFolder, "Filters"));

            string temp = _systemVars.Get<string>(MojFoldersEnum.TemporaryFolder);
            if (Directory.Exists(temp))
            {
                try { Directory.Delete(temp, true); }
                catch { }
            }
            CreateFolder(MojFoldersEnum.TemporaryFolder, temp);
        }
        private void INIData()
        {
            _userVars = new MojSystemVars(_systemVars.Get<string>(MojFoldersEnum.DatabaseFolder), "UserVars");
            _dynamicVars = new MojDynamicVars();
            _drivePool = new MojDrivePool();
            _statusVars = new MojDynamicVars();
            _tempFiles = new StaticFileCollection();
        }

        public string ApplicationFolder
        { get { return _localFolder; } }
        public void INI()
        {
            _filesDB = new FileDataBaseInterop();
            _moviesDB = new MovieDataBaseInterop();
            _langPool = new Languages_ISO639_1();
            _tempFiles = new StaticFileCollection();

            MojEnviromentVarsEnum.Inicialize();
            MojStatusVarsEnum.Inicialize();

            FileManageTools.FlushFiles();

            _pluginPool = new MojPluginPool();
        }

        public StaticFileCollection TempFiles
        { get { return _tempFiles; } }
        public MojSystemVars SystemVars
        { get { return _systemVars; } }
        public MojSystemVars UserVars
        { get { return _userVars; } }
        public MojDynamicVars DynamicVars
        { get { return _dynamicVars; } }
        public MojDynamicVars StatusVars
        { get { return _statusVars; } }
        public MojDrivePool DrivePool
        { get { return _drivePool; } }
        public FileDataBaseInterop FilesInterop
        { get { return _filesDB; } }
        public MovieDataBaseInterop MoviesInterop
        { get { return _moviesDB; } }
        public Languages_ISO639_1 LanguagePool
        { get { return _langPool; } }
        public MojPluginPool Plugins
        { get { return _pluginPool; } }

        private void CreateFolder(string FolderName, string defaultPath)
        {
            if (!_systemVars.Exist<string>(FolderName))
                _systemVars.Set<string>(FolderName, defaultPath);

            if (!Directory.Exists(_systemVars.Get<string>(FolderName)))
                Directory.CreateDirectory(_systemVars.Get<string>(FolderName));
        }
    }
    public class MojHost
    {
        private static IHost _host = null;
        public static IHost GetHost
        {
            get { return _host; }
            set { _host = value; }
        }
    }
}
